package initializ

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"grpc/internal/appconfig"
	"grpc/internal/global"
	"sync"
)

var (
	err  error
	Db   *gorm.DB
	once sync.Once
)

func InitMySQL() {
	config := appconfig.Conf.MySQLConfig

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", config.User, config.Password, config.Host, config.Port, config.Db)

	once.Do(func() {
		global.Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			fmt.Println("mysql连接失败")
			panic(err)
			return
		}
	})

	fmt.Println("mysql连接成功")

}
